package cn.tedu.crm.staff.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.tedu.crm.base.pojo.PageObject;
import cn.tedu.crm.base.response.JsonResult;
import cn.tedu.crm.dept.pojo.dto.DeptQuery;
import cn.tedu.crm.dept.pojo.vo.DeptVO;
import cn.tedu.crm.dept.service.DeptService;
import cn.tedu.crm.role.pojo.dto.RoleQuery;
import cn.tedu.crm.role.pojo.vo.RoleVO;
import cn.tedu.crm.role.service.RoleService;
import cn.tedu.crm.staff.pojo.dto.StaffParam;
import cn.tedu.crm.staff.pojo.dto.StaffQuery;
import cn.tedu.crm.staff.pojo.vo.StaffVO;
import cn.tedu.crm.staff.service.StaffService;
import cn.tedu.crm.user.pojo.dto.UserModuleQuery;
import cn.tedu.crm.user.pojo.vo.UserModuleVO;
import cn.tedu.crm.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/crm/personnel/staff")
public class StaffController {

    @Autowired
    StaffService staffService;
    @Autowired
    UserService userService;
    @Autowired
    RoleService roleService;
    @Autowired
    DeptService deptService;

    @GetMapping("select")
    @SaCheckPermission("crm:personnel:staff:select")
    public JsonResult selectStaff(StaffQuery query) {
        log.debug("查询员工信息:{}", query);
        PageObject<StaffVO> pageObject = PageObject.page(
                query.getPageNum(), query.getPageSize(), () -> staffService.selectStaff(query));
        return JsonResult.success(pageObject);
    }

    @GetMapping("selectById")
    public JsonResult selectById( Long id) {
        log.debug("根据id查询员工信息:{}", id);
        StaffVO staffVO = staffService.selectById(id);
        return JsonResult.success(staffVO);
    }


    @PostMapping("add")
    @SaCheckPermission("crm:personnel:staff:add")
    public JsonResult addStaff(@Valid @RequestBody StaffParam staffParam){
        log.debug("添加员工:{}",staffParam);
        staffService.addStaff(staffParam);
        return JsonResult.success(staffParam);
    }

    @DeleteMapping("delete")
    @SaCheckPermission("crm:personnel:staff:del")
    public JsonResult deleteStaff( Long ids) {
        log.debug("删除角色：{}",ids);
        staffService.deleteStaff(ids);
        return JsonResult.ok();
    }

    @GetMapping("users/select")
    public JsonResult selectUserModule(UserModuleQuery userModuleQuery) {
        log.debug("查询用户信息:{}", userModuleQuery);
        List<UserModuleVO> list = userService.selectUserModule(userModuleQuery);
        return JsonResult.ok(list);
    }

    @GetMapping("roles/select")
    public JsonResult selectRole(RoleQuery roleQuery) {
        log.debug("查询角色：{}", roleQuery);
        List<RoleVO> list = roleService.selectRole(roleQuery);
        return JsonResult.ok(list);
    }

    @GetMapping("dept/select")
    public JsonResult selectDept(DeptQuery query) {
        log.debug("查询部门信息:{}", query);
        List<DeptVO> list =  deptService.selectDept(query);
        return JsonResult.success(list);
    }

}
